package com.lyx.common.utils;


import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: wangsheng
 * @Date: 2021/3/27
 * Time: 14:42
 */
public class TreeUtil {


    /**
     * 构建树形
     *
     * @param treeVos
     * @param parentId
     * @param <T>
     * @return
     */
    public static <T> List<T> buildTree(List<T> treeVos, Integer parentId) {
        return buildTree(treeVos, parentId, Sets.newHashSet(), null);
    }


    /**
     * 构建树形
     *
     * @param treeVos
     * @param <T>
     * @return
     */
    public static <T> List<T> buildTree(List<T> treeVos) {
        return buildTree(treeVos, null, Sets.newHashSet(), null);
    }

    /**
     * 构建树形
     *
     * @param treeVos
     * @param parentId
     * @param <T>
     * @return
     */
    public static <T> List<T> buildTree(List<T> treeVos, Integer parentId, Comparator comparator) {

        return buildTree(treeVos, parentId, Sets.newHashSet(), comparator);
    }


    private static <T> List<T> buildTree(List<T> treeVos, Integer parentId, Set<Integer> filterIds, Comparator comparator) {
        //如果不知到parentId 需要获取parentId
        List<Integer> ids = Objects.nonNull(parentId) ? Collections.EMPTY_LIST :
                treeVos.stream().map(treeVo -> ((Tree) treeVo).getId()).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(treeVos)) {
            return treeVos;
        }
        List<T> resultTreeVos = Lists.newArrayList();
        treeVos.stream().filter(treeVo -> {
            if (treeVo instanceof Tree) {
                Tree dbTree = (Tree) treeVo;
                return !filterIds.contains(dbTree.getId()) && (Objects.isNull(parentId) ? !ids.contains(dbTree.getParentId()) : parentId.equals(dbTree.getParentId()));
            }
            return false;
        }).forEach(treeVo -> {
            Tree dbTree = (Tree) treeVo;
            filterIds.add(dbTree.getId());
            dbTree.setChildren(buildTree(treeVos, dbTree.getId(), filterIds, comparator));
            resultTreeVos.add((T) dbTree);
        });
        if (Objects.nonNull(comparator)) {
            Collections.sort(resultTreeVos, comparator);
        }
        return CollectionUtils.isNotEmpty(resultTreeVos) ? resultTreeVos : null;
    }

    /**
     * 搜索树
     *
     * @param trees   树
     * @param compare 字段
     * @return
     */
//    public static <T> List<T> search(List<T> trees, TreeCompare<T> compare) {
//
//        //判断是否为空 为空就返回空列表
//        if (CollectionUtils.isEmpty(trees)) {
//            return Lists.newArrayList();
//        }
//        boolean flag;
//        for (int i=0; i<trees.size();i++) {
//            T tree=trees.get(i);
//            if (!(tree instanceof Tree)) {
//                continue;
//            }
//            Tree<T> item = (Tree<T>) tree;
//            //判断是否匹配
//            flag = compare.compare(tree);
//            //如果当前节点匹配 就继续下一个
//            if (flag) {
//                continue;
//            }
//            //当前没有匹配 就看其孩子节点匹配不
//            List<T> children = Optional.ofNullable(item.getChildren()).orElseGet(Lists::newArrayList);
//            search(children, compare);
//            children = children.stream().filter(Objects::nonNull).collect(Collectors.toList());
//            if (CollectionUtils.isNotEmpty(children)) {
//                item.setChildren(children);
//                continue;
//            }
//            trees.set(i,null);
//        }
//        return trees.stream().filter(Objects::nonNull).collect(Collectors.toList());
//    }

}
